class GMLToggle extends Toggle
{
  Letter theLetter;
  PGraphics pg;
  boolean isRedraw;

  // not sure how to use the parent value color.active etc...
  int colorBackground_,colorActive_;
  int colorGML;

  // --------------------------------------
  public GMLToggle(ControlP5 theControlP5, Tab theParent, String theName, float theValue, float theX, float theY, int theWidth, int theHeight) 
  {
    super(theControlP5, theParent, theName, theValue, theX, theY, theWidth, theHeight);

    // See controlsP5 / CColor.java
    colorBackground_ = 0xff003652;
    colorActive_ = 0xff08a2cf;
    colorGML = 0xffffffff;

    // Display
    setDisplay( new GMLToggleDisplay() );
    redraw();
  }

  // --------------------------------------
  void setLetter(Letter l)
  {
    theLetter = l;
  }

  // --------------------------------------
  void redraw()
  {
    this.isRedraw = true;
  }


  // --------------------------------------
  // inner class to be able to access Toggle members (isActive, isOn, etc...)
  class GMLToggleDisplay implements ControllerDisplay 
  {
    public void displayBackground(PApplet theApplet, Controller theController, boolean isText) 
    {
      if (isActive) {
        theApplet.fill(colorActive_);
        //theApplet.fill(color.colorActive); // Not working, why ? ...
      } 
      else {
        if (isOn) theApplet.fill(colorActive_);
        else theApplet.fill(colorBackground_);
      }
      theApplet.rect(0, 0, width, height);

      if (isText)
      {
        theApplet.fill(colorGML);
        String s = ""+theLetter.c;
        int wText = (int)theApplet.textWidth(s);
        theApplet.text(s, (width-wText)/2, (height)/2+5);
      }
    }

    public void drawLetterGMLStrokes(PApplet theApplet)
    {
      if (isRedraw) {
        isRedraw = false;

        if (pg == null){
          pg = theApplet.createGraphics(width, height,JAVA2D);
        }

        pg.beginDraw();
          pg.background(0,0); // transparent background
          pg.smooth();
          pg.stroke(colorGML);
          GmlBrushManager gmlBrushManager = new GmlBrushManager();
          GmlLinesDrawer drawer = new GmlLinesDrawer2D(2);
          drawer.setStrokeColor(colorGML);
          gmlBrushManager.setDefault(drawer);
          gmlBrushManager.draw(pg, theLetter.gml, width);
        pg.endDraw();
      }
    }

    public void display(PApplet theApplet, Controller theController) {
      // default drawing
      if (theLetter.empty())
      {
        displayBackground(theApplet, theController, true);
      }
      // GML drawing
      else
      {
        drawLetterGMLStrokes(theApplet);
        displayBackground(theApplet, theController, false);
        theApplet.image(pg, 0, 0, width, height);
      }
    }
  }

}
